// page/index/index.js
Page({
  data:{
    isHide: 0,
    imgUrls: [],
    indicatorDots: true,
    indicatorColor: "rgba(255,255,255,.3)",
    indicatorActiveColor: "rgba(255,255,255,1)",
    autoplay: true,
    interval: 5000,
    duration: 1000,
  },
  onLoad:function(options){
    // 页面初始化 options为页面跳转所带来的参数

     // console.log(wx.getStorageSync('uid'));
      var scene = decodeURIComponent(options.scene) 
      if (scene) {
          wx.setStorageSync('pid', scene) // 存储
      }

      var app = getApp()
      var that = this;
      wx.login({
          success: function (res) {
              wx.request({
                  url: app.globalData.host + '&g=App&m=Index&a=login',
                  data: { code: res.code },
                  header: {
                      'content-type': 'application/json'
                  },
                  success: function (res1) {
                      var session_key = res1.data.session_key
                      var openid = res1.data.openid
                      wx.getUserInfo({
                          success: function (res) {
                              var userInfo = res.userInfo
                              var adheadimg = userInfo.avatarUrl
                              var adnickName = userInfo.nickName
                              var encryptedData = res.encryptedData
                              var iv = res.iv
                              wx.request({
                                  url: app.globalData.host + '&g=App&m=Index&a=userinfo', 
                                  data: {
                                      encryptedData: encryptedData, iv: iv, sessionKey: session_key, scene: scene
                                  }, header: {
                                      'content-type': 'application/json'
                                  },
                                  success: function (res) {
                                    //   console.log(res.data)
                                      var uid = parseInt(res.data)
                                      wx.setStorageSync('uid', uid) // 存储
                                      wx.setStorageSync('openid', openid) // 存储
                                      wx.setStorageSync('img', adheadimg) // 存储
                                      wx.setStorageSync('name', adnickName) // 存储
                                  }
                              })
                          }
                      })
                  }
              })
          }
      })
      // }
    //   console.log(app.globalData.host)
      wx.request({
          url: app.globalData.host + '&g=App&m=Index&a=index', 
          data: {},
          header: {
              'content-type': 'application/json'
          },
          success: function (res) {
              console.log(res.data)
              that.setData({
                  imgUrls: res.data.banner,
                  coachItem: res.data.worker,
                  courseItem: res.data.item,
                  integralItem: res.data.goods,
              })
            //   console.log(that.data.coachItem)
          }
      })
     this.setData({
      isHide: 1,
    })
  },
  onShow: function () {
    var app = getApp()
    var that = this;
    var uid = wx.getStorageSync('uid');
    wx.request({
      url: app.globalData.host + '&g=App&m=Index&a=feedback',
      data: {
        uid: uid,
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        console.log(res.data)
        var scene = res.data.scene
        that.setData({
          scene: res.data.scene
        })
      }
    })
  },
  // 分享
  onShareAppMessage: function (res) {
    var uid = wx.getStorageSync('uid');
    var that = this;
    var app = getApp()
    var scene = that.data.scene
    console.log(that.data.scene)
    console.log("1")
    if (res.from === 'button') {
      // 来自页面内转发按钮
      return {
        title: '兴祥安驾校',
        path: '/page/index/index?scene=scene',
        success: function (res) {
          // 转发成功
          wx.request({
            url: app.globalData.host + '&g=App&m=Index&a=feedback',
            data: {
              scene: scene
            },
            header: {
              'content-type': 'application/json'
            },
          })
        },
        fail: function (res) {
          // 转发失败
        }
      }
      console.log(res.target)
    }
    return {
      title: '兴祥安驾校',
      path: 'page/index/index?scene=scene',
      success: function (res) {
        // 转发成功
        wx.request({
          url: app.globalData.host + '&g=App&m=Index&a=feedback',
          data: {
            scene:scene
          },
          header: {
            'content-type': 'application/json'
          },
        })
      },
      fail: function (res) {
        // 转发失败
      }
    }
  }
})